package com.kzd.gtgame.main.game

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.BarUtils
import com.kzd.gtgame.R
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.custom.setTransitionStatus
import com.kzd.gtgame.main.KEY_GAME_DETAILS_ID
import com.kzd.gtgame.main.KEY_GAME_DETAILS_TITLE
import com.kzd.gtgame.main.home.adapter.ImageSelectAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import com.zhihu.matisse.Matisse
import kotlinx.android.synthetic.main.activity_discuss_issue.*
import java.lang.StringBuilder

class DiscussIssueActivity : OwnerViewModeActivity<BaseViewModel>() {

    private val mImageSelectAdapter = ImageSelectAdapter()

    private var mImages: MutableList<String> = mutableListOf()

    private val mTitle: String
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_TITLE) ?: ""

    private val mGameId: String
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_ID) ?: ""

    override fun viewModel(): BaseViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_discuss_issue

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flDiscussIssueBar)
    }

    override fun initView() {
        rvDiscussIssueImageBody.adapter = mImageSelectAdapter.apply {
            setNewInstance(mutableListOf(R.mipmap.photo_add_icon))
        }
        mImageSelectAdapter.setOnItemClickListener { _, _, position ->
            if (position != 0) {
                return@setOnItemClickListener
            }
            viewModel.userMatisse(activity = this, maxSelectable = 7)
        }

        flDiscussIssueBar.setTitleTextContent(mTitle)

        flDiscussIssueBar.setOnClickRightTextView {
            val content = tvDiscussIssueContent.text
            if (content.isNullOrEmpty()) {
                toast("请输入评论内容")
                return@setOnClickRightTextView
            }
            if (content.length < 10) {
                toast("评论内容不少于10个字哦~")
                return@setOnClickRightTextView
            }
            if (mImages.isNullOrEmpty()) {
                viewModel.userDiscussIssue(id = mGameId, content = content.toString())
            } else {
                viewModel.userUpload(images = mImages)
            }
        }
    }

    override fun initViewMode() {

        viewModel.userDiscussIssueLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS)
                return@observe
            toast(response.data?.message)
            finish()
        })

        viewModel.userUploadLiveData.observe(this as LifecycleOwner, { response ->
            this.setTransitionStatus(response)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            if (data.size == 0)
                return@observe
            val images = StringBuilder()
            data.forEach {
                images.append(it).append(",")
            }
            if (images.isNotEmpty()) {
                viewModel.userDiscussIssue(
                    id = mGameId,
                    content = tvDiscussIssueContent.text.toString(),
                    image = images.substring(0, images.length - 1).toString()
                )
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == RESULT_OK) {
            val pathList: MutableList<String> = Matisse.obtainPathResult(data).toMutableList()
            if (pathList.size == 0)
                return
            val images: MutableList<Any> = mutableListOf()
            images.add(R.mipmap.photo_add_icon)
            pathList.forEach {
                images.add(it)
            }
            mImageSelectAdapter.setNewInstance(images)
            mImages.clear()
            mImages.addAll(pathList)
        }
    }
}